﻿package mortal.game.view.mainUI.zazen
{
    import com.mui.controls.*;
    import com.mui.display.*;
    import extend.language.*;
    import fl.data.*;
    import flash.events.*;
    import flash.text.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.ai.singleAIs.autofight.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.map3D.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.button.*;
    import mortal.game.view.common.pageSelect.*;
    import mortal.mvc.interfaces.*;

    public class ZazenInviteWin extends BaseWindow
    {
        private var _txtInput:GTextInput;
        private var _btnSearch:GButton;
        private var _firstLine:ScaleBitmap;
        private var _list:GTileList;
        private var _page:PageSelecter;
        private var _cb:GCheckBox;
        private var _btnTips:GButton;
        private var _btnSigle:GButton;
        private var _btnAll:TimeButton;
        private var _datas:Array;
        private var _autoReply:Boolean = false;
        private static var _instance:ZazenInviteWin;

        public function ZazenInviteWin(param1:ILayer = null)
        {
            super(param1);
            this.setSize(305, 317);
            title = Language.getString(35392);
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            super.show(param1, param2);
            this.updateToNow();
            return;
        }// end function

        private function updateToNow() : void
        {
            var _loc_3:UserPlayer = null;
            var _loc_1:* = ThingUtil.entityUtil.entitysMap.getEntityByRangle(SceneRange.display);
            this._datas = [];
            var _loc_2:int = 0;
            while (_loc_2 < _loc_1.length)
            {
                
                _loc_3 = _loc_1[_loc_2] as UserPlayer;
                if (_loc_3 == null)
                {
                }
                else if (_loc_3.entityInfo.isDoubleZazen || _loc_3.sex == RolePlayer.instance.sex || _loc_3.entityInfo.entityInfo.level < GameConst.OpenZazenLevel)
                {
                }
                else
                {
                    this._datas.push(_loc_3);
                }
                _loc_2++;
            }
            AutoFightBossSelectAI.instance.updateMyPoint();
            this._datas.sort(AutoFightBossSelectAI.instance.sortByDistance);
            this.updateDatas(this._datas);
            return;
        }// end function

        public function updateDatas(param1:Array) : void
        {
            this._datas = param1;
            var _loc_2:* = Math.ceil(param1.length / 5);
            this._page.maxPage = _loc_2;
            this._page.currentPage = 1;
            this.pageChangeHandler();
            return;
        }// end function

        private function pageChangeHandler() : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:int = 0;
            var _loc_1:* = new DataProvider();
            if (this._page)
            {
                _loc_2 = (this._page.currentPage - 1) * 5;
                _loc_3 = Math.min(_loc_2 + 5, this._datas.length);
                _loc_4 = _loc_2;
                while (_loc_4 < _loc_3)
                {
                    
                    _loc_1.addItem((this._datas[_loc_4] as UserPlayer).entityInfo);
                    _loc_4++;
                }
                this._firstLine.visible = _loc_1.length > 0;
                this._list.dataProvider = _loc_1;
                this._list.drawNow();
            }
            return;
        }// end function

        public function get isAutoReply() : Boolean
        {
            return this._autoReply;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            pushUIToDisposeVec(UIFactory.bg(9, 34, 289, 216, this));
            pushUIToDisposeVec(UIFactory.label(Language.getString(35393), 20, 47, 100, 22, TextFormatAlign.LEFT, this, false, 15456951));
            this._firstLine = UIFactory.bg(12, 72, 282, 2, this, ImagesConst.SplitLine);
            this._firstLine.visible = false;
            this._txtInput = UIFactory.gTextInput(86, 44, 153, 24, this);
            this._btnSearch = UIFactory.gButton(Language.getString(35394), 239, 44, 50, 23, this);
            this._list = UIFactory.tileList(12, 74, 288, 140, this);
            this._list.setStyle("cellRenderer", ZazenPlayerRender);
            this._list.rowHeight = 29;
            this._list.columnWidth = 288;
            this._list.drawNow();
            this._page = UIFactory.pageSelecter(85, 221, this, PageSelecter.CompleteMode);
            this._cb = UIFactory.checkBox(Language.getString(35395), 17, 256, 200, 20, this);
            this._cb.selected = this._autoReply;
            this._btnTips = UIFactory.gButton(Language.getString(35396), 35, 285, 70, 22, this);
            this._btnTips.toolTipData = Language.getString(35397);
            this._btnSigle = UIFactory.gButton(Language.getString(35398), 120, 285, 70, 22, this);
            this._btnAll = UIFactory.timeButton(Language.getString(35399), 204, 285, 70, 22, this, Cache.instance.cd.ZAZEN);
            this._btnAll.cdTime = 10000;
            this._page.configEventListener(Event.CHANGE, this.onPageChange);
            this._cb.configEventListener(Event.CHANGE, this.autoReplyChange);
            this._btnSearch.configEventListener(MouseEvent.CLICK, this.searchHandler);
            this._btnSigle.configEventListener(MouseEvent.CLICK, this.inviteSingleHandler);
            this._btnAll.configEventListener(MouseEvent.CLICK, this.inviteAllHandler);
            return;
        }// end function

        private function autoReplyChange(event:Event) : void
        {
            this._autoReply = this._cb.selected;
            return;
        }// end function

        private function inviteSingleHandler(event:MouseEvent) : void
        {
            if (this._datas == null || this._datas.length == 0)
            {
                MsgManager.showRollTipsMsg(Language.getString(35400));
                return;
            }
            if (this._list.selectedItem == null)
            {
                MsgManager.showRollTipsMsg(Language.getString(35401));
                return;
            }
            var _loc_2:* = this._list.selectedItem as EntityInfo;
            GameProxy.role.inviteDoubleZazen([_loc_2.entityInfo.entityId]);
            return;
        }// end function

        private function inviteAllHandler(event:MouseEvent) : void
        {
            var _loc_3:UserPlayer = null;
            this.updateToNow();
            if (this._datas == null || this._datas.length == 0)
            {
                MsgManager.showRollTipsMsg(Language.getString(35400));
                return;
            }
            var _loc_2:Array = [];
            for each (_loc_3 in this._datas)
            {
                
                if (!_loc_3.entityInfo.isDoubleZazen)
                {
                    _loc_2.push(_loc_3.entityInfo.entityInfo.entityId);
                }
            }
            GameProxy.role.inviteDoubleZazen(_loc_2);
            return;
        }// end function

        private function searchHandler(event:MouseEvent) : void
        {
            var _loc_5:UserPlayer = null;
            this._page.maxPage = 1;
            this._page.currentPage = 1;
            var _loc_2:* = this._txtInput.text;
            var _loc_3:* = new DataProvider();
            var _loc_4:int = 0;
            while (_loc_4 < this._datas.length)
            {
                
                _loc_5 = this._datas[_loc_4] as UserPlayer;
                if (_loc_5.entityInfo.entityInfo.name.indexOf(_loc_2) >= 0)
                {
                    _loc_3.addItem(_loc_5.entityInfo);
                }
                _loc_4++;
            }
            this._page.maxPage = Math.ceil(_loc_3.length / 5);
            this._page.currentPage = 1;
            this._firstLine.visible = _loc_3.length > 0;
            this._list.dataProvider = _loc_3;
            this._list.drawNow();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._txtInput.dispose(param1);
            this._txtInput = null;
            this._btnSearch.dispose(param1);
            this._btnSearch = null;
            this._firstLine.dispose(param1);
            this._firstLine = null;
            this._list.dispose(param1);
            this._list = null;
            this._page.dispose(param1);
            this._page = null;
            this._cb.dispose(param1);
            this._cb = null;
            this._btnTips.dispose(param1);
            this._btnTips = null;
            this._btnSigle.dispose(param1);
            this._btnSigle = null;
            this._btnAll.dispose(param1);
            this._btnAll = null;
            super.disposeImpl(param1);
            return;
        }// end function

        private function onPageChange(event:Event) : void
        {
            this.pageChangeHandler();
            return;
        }// end function

        public static function hasInstance() : Boolean
        {
            return _instance != null;
        }// end function

        public static function get instance() : ZazenInviteWin
        {
            if (!_instance)
            {
                _instance = new ZazenInviteWin;
            }
            return _instance;
        }// end function

    }
}
